namespace DotNetNuke.Services.Log.EventLog
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Tabs;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Security.Roles;
    using System;
    using System.Runtime.CompilerServices;

    public class EventLogController : LogController
    {
        public void AddLog(LogInfo objEventLogInfo)
        {
            new LogController().AddLog(objEventLogInfo);
        }

        public void AddLog(PortalSettings _PortalSettings, int UserID, EventLogType objLogType)
        {
            LogProperties objProperties = new LogProperties();
            this.AddLog(objProperties, _PortalSettings, UserID, objLogType.ToString(), false);
        }

        public void AddLog(LogProperties objProperties, PortalSettings _PortalSettings, int UserID, string LogTypeKey, bool BypassBuffering)
        {
            LogController objLogController = new LogController();
            LogInfo objLogInfo = new LogInfo();
            objLogInfo.LogUserID = UserID;
            objLogInfo.LogPortalID = _PortalSettings.PortalId;
            objLogInfo.LogTypeKey = LogTypeKey;
            objLogInfo.LogProperties = objProperties;
            objLogInfo.LogPortalName = _PortalSettings.PortalName;
            objLogInfo.BypassBuffering = BypassBuffering;
            objLogController.AddLog(objLogInfo);
        }

        public void AddLog(object objCBO, PortalSettings _PortalSettings, int UserID, string UserName, EventLogType objLogType)
        {
            LogController objLogController = new LogController();
            LogInfo objLogInfo = new LogInfo();
            objLogInfo.LogUserID = UserID;
            objLogInfo.LogPortalID = _PortalSettings.PortalId;
            objLogInfo.LogTypeKey = objLogType.ToString();
            objLogInfo.LogPortalName = _PortalSettings.PortalName;
            string refStringHelperL0 = objCBO.GetType().FullName;
            if (refStringHelperL0 == "DotNetNuke.Entities.Portals.PortalInfo")
            {
                PortalInfo objPortal = (PortalInfo) objCBO;
                objLogInfo.LogProperties.Add(new LogDetailInfo("PortalID", objPortal.PortalID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("PortalName", objPortal.PortalName));
                objLogInfo.LogProperties.Add(new LogDetailInfo("Description", objPortal.Description));
                objLogInfo.LogProperties.Add(new LogDetailInfo("KeyWords", objPortal.KeyWords));
                objLogInfo.LogProperties.Add(new LogDetailInfo("LogoFile", objPortal.LogoFile));
            }
            else if (refStringHelperL0 == "DotNetNuke.Entities.Tabs.TabInfo")
            {
                TabInfo objTab = (TabInfo) objCBO;
                objLogInfo.LogProperties.Add(new LogDetailInfo("TabID", objTab.TabID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("PortalID", objTab.PortalID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("TabName", objTab.TabName));
                objLogInfo.LogProperties.Add(new LogDetailInfo("Title", objTab.Title));
                objLogInfo.LogProperties.Add(new LogDetailInfo("Description", objTab.Description));
                objLogInfo.LogProperties.Add(new LogDetailInfo("KeyWords", objTab.KeyWords));
                objLogInfo.LogProperties.Add(new LogDetailInfo("Url", objTab.Url));
                objLogInfo.LogProperties.Add(new LogDetailInfo("ParentId", objTab.ParentId.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("IconFile", objTab.IconFile));
                objLogInfo.LogProperties.Add(new LogDetailInfo("IsVisible", objTab.IsVisible.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("SkinSrc", objTab.SkinSrc));
                objLogInfo.LogProperties.Add(new LogDetailInfo("ContainerSrc", objTab.ContainerSrc));
            }
            else if (refStringHelperL0 == "DotNetNuke.Entities.Modules.ModuleInfo")
            {
                ModuleInfo objModule = (ModuleInfo) objCBO;
                objLogInfo.LogProperties.Add(new LogDetailInfo("ModuleId", objModule.ModuleID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("ModuleTitle", objModule.ModuleTitle));
                objLogInfo.LogProperties.Add(new LogDetailInfo("TabModuleID", objModule.TabModuleID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("TabID", objModule.TabID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("PortalID", objModule.PortalID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("ModuleDefId", objModule.ModuleDefID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("FriendlyName", objModule.FriendlyName));
                objLogInfo.LogProperties.Add(new LogDetailInfo("IconFile", objModule.IconFile));
                objLogInfo.LogProperties.Add(new LogDetailInfo("Visibility", objModule.Visibility.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("ContainerSrc", objModule.ContainerSrc));
            }
            else if (refStringHelperL0 == "DotNetNuke.Entities.Users.UserInfo")
            {
                UserInfo objUser = (UserInfo) objCBO;
                objLogInfo.LogProperties.Add(new LogDetailInfo("UserID", objUser.UserID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("FirstName", objUser.Profile.FirstName));
                objLogInfo.LogProperties.Add(new LogDetailInfo("LastName", objUser.Profile.LastName));
                objLogInfo.LogProperties.Add(new LogDetailInfo("UserName", objUser.Membership.Username));
                objLogInfo.LogProperties.Add(new LogDetailInfo("Email", objUser.Membership.Email));
            }
            else if (refStringHelperL0 == "DotNetNuke.Security.Roles.RoleInfo")
            {
                RoleInfo objRole = (RoleInfo) objCBO;
                objLogInfo.LogProperties.Add(new LogDetailInfo("RoleID", objRole.RoleID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("RoleName", objRole.RoleName));
                objLogInfo.LogProperties.Add(new LogDetailInfo("PortalID", objRole.PortalID.ToString()));
                objLogInfo.LogProperties.Add(new LogDetailInfo("Description", objRole.Description));
                objLogInfo.LogProperties.Add(new LogDetailInfo("IsPublic", objRole.IsPublic.ToString()));
            }
            else
            {
                objLogInfo.LogProperties.Add(new LogDetailInfo("logdetail", XmlUtils.Serialize(RuntimeHelpers.GetObjectValue(objCBO))));
            }
            objLogController.AddLog(objLogInfo);
        }

        public void AddLog(string PropertyName, string PropertyValue, PortalSettings _PortalSettings, int UserID, EventLogType objLogType)
        {
            LogProperties objProperties = new LogProperties();
            LogDetailInfo objLogDetailInfo = new LogDetailInfo();
            objLogDetailInfo.PropertyName = PropertyName;
            objLogDetailInfo.PropertyValue = PropertyValue;
            objProperties.Add(objLogDetailInfo);
            this.AddLog(objProperties, _PortalSettings, UserID, objLogType.ToString(), false);
        }

        public void AddLog(string PropertyName, string PropertyValue, PortalSettings _PortalSettings, int UserID, string LogType)
        {
            LogProperties objProperties = new LogProperties();
            LogDetailInfo objLogDetailInfo = new LogDetailInfo();
            objLogDetailInfo.PropertyName = PropertyName;
            objLogDetailInfo.PropertyValue = PropertyValue;
            objProperties.Add(objLogDetailInfo);
            this.AddLog(objProperties, _PortalSettings, UserID, LogType, false);
        }

        public enum EventLogType
        {
            USER_CREATED,
            USER_DELETED,
            LOGIN_SUPERUSER,
            LOGIN_SUCCESS,
            LOGIN_FAILURE,
            CACHE_REFRESHED,
            PASSWORD_SENT_SUCCESS,
            PASSWORD_SENT_FAILURE,
            LOG_NOTIFICATION_FAILURE,
            PORTAL_CREATED,
            PORTAL_DELETED,
            TAB_CREATED,
            TAB_UPDATED,
            TAB_DELETED,
            TAB_SENT_TO_RECYCLE_BIN,
            TAB_RESTORED,
            USER_ROLE_CREATED,
            USER_ROLE_DELETED,
            ROLE_CREATED,
            ROLE_UPDATED,
            ROLE_DELETED,
            MODULE_CREATED,
            MODULE_UPDATED,
            MODULE_DELETED,
            MODULE_SENT_TO_RECYCLE_BIN,
            MODULE_RESTORED,
            SCHEDULER_EVENT_STARTED,
            SCHEDULER_EVENT_PROGRESSING,
            SCHEDULER_EVENT_COMPLETED,
            APPLICATION_START,
            APPLICATION_END,
            APPLICATION_SHUTTING_DOWN,
            SCHEDULER_STARTED,
            SCHEDULER_SHUTTING_DOWN,
            SCHEDULER_STOPPED,
            ADMIN_ALERT,
            HOST_ALERT
        }
    }
}

